#include "bsp_magnetic.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"

QueueHandle_t mag_220v_queue =NULL;

void magnetic_init(void)
{
    /* 虚实管脚 */
    gpio_config_t io_conf = {
        .intr_type = GPIO_INTR_DISABLE,
        .mode =  GPIO_MODE_DEF_OUTPUT,
        .pin_bit_mask = GPIO_CL_PIN_SEL,
        .pull_down_en = 1,                     // 配置下拉 默认 实震模式 模式
        .pull_up_en = 0,
    };
    gpio_config(&io_conf);
    gpio_set_level(GPIO_CL_220V_PORT1, 0);
    gpio_set_level(GPIO_XUSHI_PORT1, 0);
    gpio_set_level(LEDC_OUTPUT_IO1, 0);
    mag_220v_queue = xQueueCreate(5, sizeof(uint32_t));
    // Prepare and then apply the LEDC PWM timer configuration
    // ledc_timer_config_t mainpwm_timer = {
    //     .speed_mode       = LEDC_MODE,
    //     .duty_resolution  = LEDC_DUTY_RES,
    //     .timer_num        = LEDC_TIMER,
    //     .freq_hz          = LEDC_FREQUENCY,  
    //     .clk_cfg          = LEDC_AUTO_CLK,
    // };
    // ESP_ERROR_CHECK(ledc_timer_config(&mainpwm_timer));

    // // Prepare and then apply the LEDC PWM channel configuration
    // ledc_channel_config_t mainpwm_channel1 = {
    //     .speed_mode     = LEDC_MODE,
    //     .channel        = LEDC_CHANNEL1,
    //     .timer_sel      = LEDC_TIMER,
    //     .intr_type      = LEDC_INTR_DISABLE,
    //     .gpio_num       = LEDC_OUTPUT_IO1,
    //     .duty           = 0, // Set duty to 0%
    //     .hpoint         = 0
    // };
    // ESP_ERROR_CHECK(ledc_channel_config(&mainpwm_channel1));
}

void magnetic_xushi_set(uint8_t xushi_status)
{
    if(xushi_status == SHI_MODE){
        gpio_set_level(GPIO_XUSHI_PORT1, 0);
    }else if(xushi_status == XU_MODE){
        gpio_set_level(GPIO_XUSHI_PORT1, 1);
    }
}

static void  magnetic_set_duty(uint16_t duty)
{
    // Set duty to 50%
    ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL1, duty));
    // Update duty to apply the new value
    ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL1));
}




static void magnetic_set_freq(uint32_t freq)
{
    ledc_timer_config_t timer_cfg = {
        .speed_mode       = LEDC_MODE,
        .duty_resolution  = LEDC_DUTY_RES,
        .timer_num        = LEDC_TIMER,
        .freq_hz          = freq,  
        .clk_cfg          = LEDC_AUTO_CLK,
    };
    
    esp_err_t ret = ledc_timer_config(&timer_cfg);
    if(ret != ESP_OK) {
        printf("设置频率失败! 错误码: 0x%x\n", ret);
    }
}

static uint32_t send_status = 0;
void magnetic_output_mode(uint8_t output_mode)
{
    switch (output_mode)
    {
    case MAINPWM_NORMAL:
        send_status = MAG_220V_NORMAL;
        xQueueSend(mag_220v_queue, &send_status, 0);
        break;
    case MAINPWM_DUTYCHANGE:
        send_status = MAG_220V_DUTYCHANGE;
        xQueueSend(mag_220v_queue, &send_status, 0);
        break;
    case MAINPWM_STOP:
        send_status = MAG_220V_STOP;
        xQueueSend(mag_220v_queue, &send_status, 0);
        break;   
    default:
        break;
    }
    
}




